<?php
/**
 * @description:
 * @author sunnyw<iwisunny@gmail.com>
 */
class Kiosk_status_model extends MY_Model
{

    protected $_table='kiosk_status';
    protected $return_type='array';
    protected $primary_key='kiosk_id';

    protected $ci=null;

    //今日网卡流量是否保存
    const KEY_BYTES_SAVED='today_net_bytes_saved';

    function __construct()
    {
        parent::__construct();

        $this->ci=get_instance();
    }

    function flush_row($id, $data){
        $chk_row=$this->get($id);
        $st=false;

        $data['update_time']=time();

        $last_accum_recv = $last_accum_send = 0;

        //每天凌晨结算当天的网卡流量
        $hour=date('H', time());
        if($hour == '00'){
            $net_saved=$this->ci->cache->file->get(self::KEY_BYTES_SAVED);

            if(false===$net_saved){
                //summary today net card bytes
                $last_accum_recv= empty($chk_row) ? 0 : intval($chk_row['net_total_recv_bytes']);
                $last_accum_send= empty($chk_row) ? 0 : intval($chk_row['net_total_send_bytes']);

                $this->ci->cache->file->save(self::KEY_BYTES_SAVED, 1, 3600);
            }
        }

        //将字节数转换成kb，避免32位整形相加的上溢误差
        $data['net_total_recv_bytes']=intval( intval($data['net_recv_bytes'])/1024 + intval($last_accum_recv)/1024 );
        $data['net_total_send_bytes']=intval( intval($data['net_send_bytes'])/1024 + intval($last_accum_send)/1024 );

        if(empty($chk_row)){
            //register a new kiosk machine
            $data[$this->primary_key]=$id;
            $st=$this->insert($data, true);
        }
        else{
            $st=$this->update($id, $data, true);
        }

        return $st !== false;
    }

}